﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using Model;

namespace LOPABusiness
{
    public class Workspace{
        public Workspace(){}
        private LopaProject lopaProject = LopaProject.LopaProjectInstance;


        public LopaProject LopaProject{
            get { return lopaProject; }
            set { lopaProject = value; }
        }

        public void InitProjectInstance(string comName, string proName, string proLoc, string TecUni, string type)
        {
            NewProjectDetails projectDetails = new NewProjectDetails(comName,proName,proLoc,TecUni,type);
            lopaProject.ProjectDetails = projectDetails;
        }

        public LOPAWorkNode GetWorkNodeByNodeName(string name){
            foreach (LOPAWorkNode node in lopaProject.LopaWorkNodeList){
                if (node.EventName.Equals(name)){
                    return node;
                }
            }
            return null;
        }

        public Dictionary<string,double> GetSafeFreList(){
            Dictionary<string ,double> dic = new Dictionary<string, double>();
            double value;
            foreach (RiskCriteriaItem item in lopaProject.RiskCriteria.SafeCriterias){
                value = 0;
                if (item.Frequence != null){   
                    double.TryParse(item.Frequence, out value);
                    dic.Add(item.SafeType, value);
                }
                else{
                    dic.Add(item.SafeType,-1);
                }
            }
            return dic;
        }

        public Dictionary<string, double> GetEnvFreList()
        {
            Dictionary<string, double> dic = new Dictionary<string, double>();
            double value;
            foreach (RiskCriteriaItem item in lopaProject.RiskCriteria.EnvCriterias)
            {
                value = 0;
                if (item.Frequence != null){
                    double.TryParse(item.Frequence, out value);
                    dic.Add(item.SafeType, value);
                }
                else{
                    dic.Add(item.SafeType, -1);
                }
            }
            return dic;
        }

        public Dictionary<string, double> GetBusinessFreList()
        {
            Dictionary<string, double> dic = new Dictionary<string, double>();
            double value;
            foreach (RiskCriteriaItem item in lopaProject.RiskCriteria.BusinessCriterias)
            {
                value = 0;
                if (item.Frequence != null){
                    double.TryParse(item.Frequence, out value);
                    dic.Add(item.SafeType, value);
                }
                else{
                    dic.Add(item.SafeType, -1);
                }
            }
            return dic;
        }

        public void AddNewWorkNode(LOPAWorkNode workNode){
            LopaProject.LopaWorkNodeList.Add(workNode);
        }

        public void SaveProject(string fileName){
            BinaryFormatter binaryFormtter = new BinaryFormatter();
            Stream fstream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
            binaryFormtter.Serialize(fstream,LopaProject);
            fstream.Close();
        }

        public LopaProject OpenProject(string fileName)
        {
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            Stream fstream = File.OpenRead(fileName);
            lopaProject = (LopaProject)binaryFormatter.Deserialize(fstream);
            fstream.Close();
            return lopaProject;
        }

        public event Action<Dictionary<string, string>> PidListChanged;
        public void OnPidListChanged(Dictionary<string, string> d)
        {
            if (PidListChanged != null)
            {
                PidListChanged(d);
            }
        }

        public event Action<List<string>> SafeListChanged;
        public event Action<List<string>> EnvListChanged;
        public event Action<List<string>> BusinessListChanged;
        public void OnSafeListChanged(List<string> list){
            if (SafeListChanged != null){
                SafeListChanged(list);
            }
        }
        public void OnEnvListChanged(List<string> list)
        {
            if (EnvListChanged != null)
            {
                EnvListChanged(list);
            }
        }
        public void OnBusinessListChanged(List<string> list)
        {
            if (BusinessListChanged != null)
            {
                BusinessListChanged(list);
            }
        }
        public List<string> GetPidNumList()
        {
            List<string> res = new List<string>();
            foreach (PID pid in lopaProject.PidList)
            {
                res.Add(pid.DrawingNum);
            }
            return res;
        }

        public List<string> GetPsiList()
        {
            return lopaProject.PsiList;
        }

        public void SavePsiList(List<string> list)
        {
            lopaProject.PsiList.Clear();
            lopaProject.PsiList = list;
        }

        public bool HasNode(string name)
        {
            foreach (LOPAWorkNode workNode in lopaProject.LopaWorkNodeList)
            {
                if (workNode.EventName.Equals(name))
                {
                    return true;
                }
            }
            return false;
        }
        private static List<string> _titleList = new List<string>();

        public List<string> TitleList{
            get { return _titleList; }
            set { _titleList = value; }
        }

        static Workspace(){
            _titleList = DataAccess.CSVHelper.GetTitleList();
        }

    }
}
